//
//  Sky72ReservationViewController.m
//  Sky72
//
//  Created by ladaka on 13. 9. 26..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "Sky72ReservationViewController.h"

@interface Sky72ReservationViewController ()

@end

@implementation Sky72ReservationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    self.svScrollView.scrollEnabled = YES;
//    self.svScrollView.zoomScale = 1;
    
    UIImage *testImage = [UIImage imageNamed:@"testImage.png"];
    self.ivImageView.image = testImage;
    
//    // 이미지를 해상도 비율에 맞게 리사이징
//    self.ivImageView.contentMode = UIViewContentModeScaleAspectFit;
//    // 확대/축소시 비율을 맞게 설정
//    self.ivImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//    // 이미지뷰어의 크기를 설정
    [self.ivImageView setFrame:CGRectMake(0, 0, testImage.size.width, testImage.size.height)];
//
//    self.svScrollView.contentSize = CGSizeMake(self.ivImageView.image.size.width, self.ivImageView.image.size.height);
    
    self.svScrollView.contentSize = testImage.size;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
//    CGRect scrollViewFrame = self.svScrollView.frame;
//    CGFloat scaleWidth = scrollViewFrame.size.width / self.svScrollView.contentSize.width;
//    CGFloat scaleHeight = scrollViewFrame.size.height / self.svScrollView.contentSize.height;
//    CGFloat minScale = MIN(scaleWidth, scaleHeight);
//    self.svScrollView.minimumZoomScale = minScale;
//    
//    self.svScrollView.maximumZoomScale = 1.0f;
//    self.svScrollView.zoomScale = minScale;
    
    [self centerScrollViewContents];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setIvImageView:nil];
    [self setSvScrollView:nil];
    [super viewDidUnload];
}

- (void)centerScrollViewContents
{
    // soho
//    CGFloat offsetY = 0;
//    CGFloat offsetX = 0;
//    CGFloat contentWidth = self.svScrollView.contentSize.width;
//    CGFloat contentHeight = self.svScrollView.contentSize.height;
//    CGFloat boundsWidth = self.svScrollView.bounds.size.width;
//    CGFloat boundsHeight = self.svScrollView.bounds.size.height;
//    
//    if((contentWidth - boundsWidth) > (contentHeight - boundsHeight)) {
//        offsetX = (contentWidth - boundsWidth) * 0.5;
//    } else if((contentWidth - boundsWidth) < (contentHeight - boundsHeight)) {
//        offsetY = (contentHeight - boundsHeight) * 0.5;
//    } else {
//        if((contentWidth - boundsWidth) > (contentHeight - boundsHeight)) {
//            offsetX = -(boundsWidth - contentWidth) * 0.5;
//        } else {
//            offsetY = -(boundsHeight - contentHeight) * 0.5;
//        }
//    }
//    
//    [self.svScrollView setContentOffset:CGPointMake(offsetX, offsetY) animated:NO];
    
//    CGSize boundsSize = self.svScrollView.bounds.size;
//    CGRect contentsFrame = self.ivImageView.frame;
//    
//    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
//        contentsFrame = CGRectMake(0, 0,
//                                   (self.svScrollView.frame.size.height * self.ivImageView.image.size.width)/self.ivImageView.image.size.height * self.svScrollView.zoomScale,
//                                   self.svScrollView.frame.size.height * self.svScrollView.zoomScale);
//    } else {
//        contentsFrame = CGRectMake(0, 0,
//                                   self.svScrollView.frame.size.width * self.svScrollView.zoomScale,
//                                   (self.svScrollView.frame.size.width * self.ivImageView.image.size.height)/self.ivImageView.image.size.width * self.svScrollView.zoomScale);
//    }
//    
//    if (contentsFrame.size.width < boundsSize.width) {
//        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
//    } else {
//        contentsFrame.origin.x = 0.0f;
//    }
//    
//    if (contentsFrame.size.height < boundsSize.height) {
//        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
//    } else {
//        contentsFrame.origin.y = 0.0f;
//    }
//    
//    self.ivImageView.frame = contentsFrame;
//    self.svScrollView.contentSize = self.ivImageView.frame.size;
    
    
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.ivImageView;
}

@end
